package com.example.service.impl;

import com.example.entity.ParkingRecord;
import com.example.service.ParkingFeeConfigService;
import com.example.service.ParkingFeeService;
import com.example.strategy.ParkingFeeStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Optional;


// 计费服务实现
@Service
public class ParkingFeeServiceImpl implements ParkingFeeService {

    @Autowired
    private ParkingFeeConfigService configService;

    @Override
    public BigDecimal calculateFee(ParkingRecord record, Long configId) {
        return Optional.ofNullable(configService.getById(configId))
                .map(config -> {
                    ParkingFeeStrategy strategy = getStrategy(config)
                            .andThen(ParkingFeeStrategy.freeTimeStrategy(record, config))
                            .andThen(ParkingFeeStrategy.overnightFeeStrategy(record, config))
                            .andThen(ParkingFeeStrategy.maxFeeStrategy(config));

                    return strategy.calculateFee(record, config);
                })
                .orElseThrow(() -> new IllegalArgumentException("Invalid config ID: " + configId));
    }
}
